package com.enjoyor.soa.traffic.core.tpcms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tpcms.domain.InfoAlarmRedis;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoWhiteList;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoAlarmRedisMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoWhiteListMapper;
import com.enjoyor.soa.traffic.core.tpcms.service.IWhiteListService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.frame.support.redis.IRedisTool;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

/**
 * 白名单车辆数据操作
 * 
 * @author zsy 2018年4月27日
 *
 */
@Service
@Transactional
public class WhiteListService implements IWhiteListService {

    @Autowired
    private IInfoWhiteListMapper whiteListMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @SuppressWarnings("unused")
    @Autowired
    private IInfoAlarmRedisMapper infoAlarmRedisMapper;

    @Autowired
    private IRedisTool redisTool;

    @Value("${log.system.name}")
    private String systemName;

    @Override
    @SuppressWarnings("rawtypes")
    public Page getWhiteListPage(Page page) {
        return whiteListMapper.selectPageInfo(page);
    }

    @Override
    public List<InfoWhiteList> getWhiteListList(Map<String, Object> map) {
        return (List<InfoWhiteList>) whiteListMapper.selectListInfo(map);
    }

    /**
     * 白名单车辆新增
     */
    @SuppressWarnings("unused")
    @Override
    public int insertWhiteList(InfoWhiteList infoWhiteList, String userName) {
        infoWhiteList.setWhiteListId("W" + TimeHelper.getUniqueString());
        infoWhiteList.setRecordTime(TimeHelper.dateToLongString());
        infoWhiteList.setIsDelete("0");
        int unique = checkVehicleUnique(infoWhiteList.getLicenseNumber());
        if (unique > 0) {
            return 1;
        } else {
            // 新增白名单
            whiteListMapper.insert(infoWhiteList);
            // 存储redis
            Boolean redisResult = this.setRedisByWhite(infoWhiteList);
            // 保存日志
            pubLogMapper.saveNoramlLogInfo(userName, systemName,
                    LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                    String.format("添加InfoWhiteList表单编号:{%s} 操作成功!", new Object[] { infoWhiteList.getWhiteListId() }),
                    LogInfoDic.LOG_MODULAR_ENUM.WHITE_LIST_MANAGEMENT.getMessage());
            return 0;
        }
    }

    /**
     * 白名单车辆修改
     */
    @SuppressWarnings("unused")
    @Override
    public void editWhiteList(InfoWhiteList infoWhiteList, String userName) {
        infoWhiteList.setRecordTime(TimeHelper.dateToLongString());
        infoWhiteList.setIsDelete("0");
        whiteListMapper.updateById(infoWhiteList);
        // 存储redis
        Boolean redisResult = this.setRedisByWhite(infoWhiteList);
        // 保存日志
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改InfoWhiteList表单编号:{%s} 操作成功!", new Object[] { infoWhiteList.getWhiteListId() }),
                LogInfoDic.LOG_MODULAR_ENUM.WHITE_LIST_MANAGEMENT.getMessage());
    }

    /**
     * 删除白名单车辆
     */
    @SuppressWarnings("unused")
    @Override
    public void deleteWhiteList(String whiteListId, String userName) {
        // 查询白名单信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("whiteListId", whiteListId);
        List<InfoWhiteList> infoWhite = whiteListMapper.selectListInfo(map);

        InfoWhiteList infoWhiteList = new InfoWhiteList();
        infoWhiteList.setWhiteListId(whiteListId);
        infoWhiteList.setIsDelete("1");
        infoWhiteList.setRecordTime(TimeHelper.dateToLongString());
        whiteListMapper.updateByIdSelective(infoWhiteList);
        // 移除redis
        if (infoWhite.size() > 0) {
            String licenseNumber = infoWhite.get(0).getLicenseNumber();
            Boolean redisDelete = redisTool.del(licenseNumber);
            // infoAlarmRedisMapper.deleteRedisByVehicle(licenseNumber);
        }
        // 保存日志
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
                String.format("删除InfoWhiteList表单编号:{%s} 操作成功!", new Object[] { whiteListId }),
                LogInfoDic.LOG_MODULAR_ENUM.WHITE_LIST_MANAGEMENT.getMessage());

    }

    /**
     * 批量导入
     */
    @SuppressWarnings("unused")
    public List<Integer> insertBatch(List<InfoWhiteList> whiteLists, String userName) {
        List<Integer> resultNum = new ArrayList<Integer>();
        for (InfoWhiteList infoWhiteList : whiteLists) {
            infoWhiteList.setWhiteListId("W" + TimeHelper.getUniqueString());
            infoWhiteList.setRecordTime(TimeHelper.dateToLongString());
            infoWhiteList.setIsDelete("0");
            int unique = checkVehicleUnique(infoWhiteList.getLicenseNumber());
            if (unique > 0) {
                resultNum.add(1);
            } else {
                resultNum.add(0);
                whiteListMapper.insert(infoWhiteList);
                // 存储redis
                Boolean redisResult = this.setRedisByWhite(infoWhiteList);
                // 保存日志
                pubLogMapper.saveNoramlLogInfo(userName, systemName,
                        LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                        String.format("添加InfoWhiteList表单编号:{%s} 操作成功!",
                                new Object[] { infoWhiteList.getWhiteListId() }),
                        LogInfoDic.LOG_MODULAR_ENUM.WHITE_LIST_MANAGEMENT.getMessage());
            }
        }
        return resultNum;
    }

    /**
     * 校验车辆是否唯一
     * 
     * @param licenseNumber
     */
    private int checkVehicleUnique(String licenseNumber) {
        List<InfoWhiteList> list = whiteListMapper.selectByLicenseNumber(licenseNumber);
        return list.size();
    }

    /**
     * 根据白名单存储redis以及更新redis数据表
     * 
     * @param infoWhiteList
     *            白名单信息
     */
    private Boolean setRedisByWhite(InfoWhiteList infoWhiteList) {
        String key = infoWhiteList.getLicenseNumber();
        InfoAlarmRedis redisValue = new InfoAlarmRedis();
        redisValue.setType("2");
        redisValue.setVehicleTypeName(infoWhiteList.getVehicleTypeName());
        Boolean redisResult = redisTool.setObject(key, redisValue);

        // redisValue.setLicenseNumber(key);
        // infoAlarmRedisMapper.insertAlarmRedis(redisValue);
        return redisResult;
    }
}
